package com.ym.manager.admin.act;

import com.ym.manager.admin.act.model.Act;
import com.ym.manager.admin.goods.model.Goods;
import com.ym.manager.sys.model.AdminUser;
import com.ym.utils.Common;
import com.ym.utils.DateUtil;
import com.ym.utils.Pages;
import com.ym.utils.ResultUtil;
import com.ym.web.bind.CurrentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@Controller
@RequestMapping("/act/")
public class ActController {

    @Autowired
    private ActService actService;


    /**
     * 页面：活动列表
     * @return
     */
    @RequestMapping("list")
    public String list(){
        return "/act/list";
    }

    /**
     * 功能：活动列表
     * @return
     */
    @RequestMapping("listData")
    @ResponseBody
    public String listData(@CurrentUser AdminUser user,
                           @RequestParam(defaultValue = "0") int page,
                           @RequestParam(defaultValue = "10") int limit,
                           @RequestParam(defaultValue = "") String name,
                           @RequestParam(defaultValue = "-1") int status,
                           @RequestParam(defaultValue = "1") int type){
        Map<String,Object> map = ResultUtil.result();
        try {
            Pages<Act> pages = actService.listData(page,limit,user.getMerchant_id(),name,status,type);
            //查询数据
            map.put("data",pages.getList());
            map.put("count",pages.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            map.put("code",-1000);
            map.put("msg","系统异常");
        }
        return ResultUtil.toJSON(map);
    }

    /**
     * 页面：新增活动
     * @return
     */
    @GetMapping("add")
    public String addAct(){
        return "/act/add";
    }

    /**
     * 页面：获取活动列表
     * @return
     */
    @GetMapping("addActGoods")
    public String addActGoods(Model model, @RequestParam(defaultValue = "") String id){
        if(Common.isNotEmpty(id)){
            model.addAttribute("ids", Arrays.asList(id.split(",")));
        }
        return "/act/addActGoods";
    }

    /**
     * 功能：获取活动列表
     * @param name 产品名称
     * @param categoryId 类目ID
     * @return
     */
    @PostMapping("addActGoods")
    @ResponseBody
    public String addActGoods(@RequestParam(defaultValue = "")String name,
                             @RequestParam(defaultValue = "0")int categoryId,
                             @RequestParam(defaultValue = "1") int page,
                             @RequestParam(defaultValue = "20")int limit){
        Map<String,Object> result = ResultUtil.result();
        try {
            Pages<Goods> pages = actService.addActGoods(page,limit,name,categoryId);
            //查询数据
            result.put("data",pages.getList());
            result.put("count",pages.getTotal());
            result.put("recordsFiltered","");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：新增活动
     * @param act
     * @return
     */
    @PostMapping("add")
    @ResponseBody
    public String addAct(Act act,
                         @CurrentUser AdminUser adminUser){
        Map<String,Object> result = ResultUtil.result();
        act.setMerchant_id(adminUser.getMerchant_id());
        act.setCreate_time(DateUtil.getDate(new Date(),"yyyy-MM-dd HH:mm:ss"));
        try {
            int i;
            if(act.getId()>0){
                i = actService.update(act);
                result.put("code",i>1?0:-1);
                result.put("msg",i>1?"修改成功":i==1?"修改失败，商品关联错误":"修改失败，请联系管理员");
            }else{
                i = actService.insert(act);
                result.put("code",i>1?0:-1);
                result.put("msg",i>1?"新增成功":i==1?"新增失败，商品关联错误":"新增失败，请联系管理员");
            }
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 页面：修改活动
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Model model,int id){
        model.addAttribute("promote",actService.detail(id));
        return "/act/edit";
    }

    /**
     * 功能：活动上下架
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("shelf")
    @ResponseBody
    public String shelf(@RequestParam(defaultValue = "0") int id,
                        @RequestParam(defaultValue = "0")int status){
        Map<String,Object> result = ResultUtil.result();
        if(id<=0 || status <=0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = actService.shelf(id,status);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?status==1?"下线成功":"上线成功":i==0?"下线失败，请联系管理员":"上线失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }

    /**
     * 功能：删除活动
     * @param id
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public String delete(@RequestParam(defaultValue = "0") int id){
        Map<String,Object> result = ResultUtil.result();
        if(id<=0){
            result.put("code",-1000);
            result.put("msg","请求参数异常");
            return ResultUtil.toJSON(result);
        }
        try {
            int i = actService.delete(id);
            result.put("code",i>0?0:-1);
            result.put("msg",i>0?"删除成功":"删除失败，请联系管理员");
        }catch (Exception e){
            e.printStackTrace();
            result.put("code",-1000);
            result.put("msg","系统异常");
        }
        return ResultUtil.toJSON(result);
    }
}
